package org.example.draw;

import org.example.bean.Cord;
import org.example.bean.Grid;

import javax.swing.*;
import java.awt.*;
import java.util.List;

import java.util.ArrayList;
import java.util.Random;

public class MapDrawing extends JFrame {
    private static final int CELL_SIZE = 1; // 单元格大小（放大便于观察）
    private int[][] maps;
    private List<List<Cord>> allPaths; // 存储所有已添加的路径
    private List<Color> pathColors;     // 为每条路径分配不同颜色
    private final Random random = new Random();

    private int width, height;

    public MapDrawing(Grid[][] grids, String name) {
        this.maps = new int[grids.length][grids[0].length];
        for (int i = 0; i < grids.length; i++) {
            for (int j = 0; j < grids[0].length; j++) {
                Grid grid = grids[i][j];
                if (grid != null && grid.isStopBlock()) {
                    maps[j][i] = 1;
                }
            }
        }

        this.allPaths = new ArrayList<>();
        this.pathColors = new ArrayList<>();

        this.height = maps.length;
        this.width = maps[0].length;

        setTitle(name);
        setSize(width * CELL_SIZE, height * CELL_SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 添加绘图面板
        add(new DrawingPanel());
    }

    /**
     * 添加一条新路径，使用随机颜色显示
     */
    public void addPath(List<Cord> path) {
        if (path == null || path.isEmpty()) return;
        // 为新路径生成一个随机颜色（避免黑色、白色等难辨色）
        Color color = generateRandomColor();
        pathColors.add(color);
        allPaths.add(new ArrayList<>(path)); // 深拷贝路径
        // 请求重绘
        SwingUtilities.invokeLater(() -> {
            repaint();
        });
    }

    /**
     * 生成一个适合显示的随机颜色
     */
    private Color generateRandomColor() {
        // 避免太接近黑色、白色、灰色
        int r = 50 + random.nextInt(200);
        int g = 50 + random.nextInt(200);
        int b = 50 + random.nextInt(200);
        return new Color(r, g, b);
    }

    /**
     * 清除所有路径
     */
    public void clearPaths() {
        allPaths.clear();
        pathColors.clear();
        repaint();
    }

    private class DrawingPanel extends JPanel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            // 绘制网格线
            g2d.setColor(Color.LIGHT_GRAY);
            for (int y = 0; y <= height; y++) {
                g2d.drawLine(0, y * CELL_SIZE, width * CELL_SIZE, y * CELL_SIZE);
            }
            for (int x = 0; x <= width; x++) {
                g2d.drawLine(x * CELL_SIZE, 0, x * CELL_SIZE, height * CELL_SIZE);
            }
            // 绘制障碍物
            g2d.setColor(Color.BLACK);
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (maps[y][x] == 1) { // 障碍物
                        g2d.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    }
                }
            }
            // 绘制所有已添加的路径
            for (int i = 0; i < allPaths.size(); i++) {
                List<Cord> path = allPaths.get(i);
                Color color = pathColors.get(i);
                g2d.setColor(color);
                // 画路径连线
                if (path.size() > 1) {
                    Cord prev = path.get(0);
                    for (int j = 1; j < path.size(); j++) {
                        Cord curr = path.get(j);
                        g2d.drawLine(
                                prev.getX() * CELL_SIZE + CELL_SIZE / 2,
                                prev.getY() * CELL_SIZE + CELL_SIZE / 2,
                                curr.getX() * CELL_SIZE + CELL_SIZE / 2,
                                curr.getY() * CELL_SIZE + CELL_SIZE / 2
                        );
                        prev = curr;
                    }
                }
            }
        }
    }
}
